/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef COMMON_MODULE_REGISTRY_H
#define COMMON_MODULE_REGISTRY_H

#include <functional>
#include "utils_callback_list.h"

namespace El {
namespace Common {

// 模块初始化回调函数类型
using ModuleInitCallback = std::function<bool()>;

/**
 * @brief 模块注册管理器类
 */
class ModuleRegistry {
public:
    /**
     * @brief 获取ModuleRegistry单例
     * @return ModuleRegistry单例引用
     */
    static ModuleRegistry& GetInstance();

    /**
     * @brief 注册模块初始化回调函数
     * @param callback 模块初始化回调函数，返回true表示初始化成功，false表示失败
     * @param priority 优先级，数值越小优先级越高，默认为100
     * @return 返回注册句柄，用于后续取消注册，-1表示注册失败
     */
    int32_t RegisterModule(const ModuleInitCallback& callback, int32_t priority = 100);

    /**
     * @brief 取消注册模块初始化回调函数
     * @param handle 注册时返回的句柄
     */
    void UnregisterModule(int32_t handle);

    /**
     * @brief 初始化所有注册的模块
     * @return true表示所有模块初始化成功，false表示有模块初始化失败
     */
    bool InitializeAllModules();

    /**
     * @brief 清空所有注册的模块回调
     */
    void ClearAllModules();

    /**
     * @brief 检查是否有注册的模块
     * @return true表示有注册的模块，false表示没有
     */
    bool HasRegisteredModules() const;

private:
    ModuleRegistry() = default;
    ~ModuleRegistry() = default;
    ModuleRegistry(const ModuleRegistry&) = delete;
    ModuleRegistry& operator=(const ModuleRegistry&) = delete;

private:
    El::Utils::CallbackList<bool> moduleCallbacks_;
};

} // namespace Common
} // namespace El

#endif // COMMON_MODULE_REGISTRY_H